package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.*;
import com.jai.pms.domain.Process;
import com.jai.pms.mapper.*;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.service.IProjectDataProcessService;

/**
 * 项目数据工序Service业务层处理
 *
 * @author v
 * @date 2023-10-09
 */
@Service
public class ProjectDataProcessServiceImpl implements IProjectDataProcessService {
    @Autowired
    private ProjectDataProcessMapper projectDataProcessMapper;

    @Autowired
    private ProjectDataMapper projectDataMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private ProcessMapper processMapper;

    /**
     * 查询项目数据工序
     *
     * @param id 项目数据工序主键
     * @return 项目数据工序
     */
    @Override
    public ProjectDataProcess selectProjectDataProcessById(Long id) {
        return projectDataProcessMapper.selectProjectDataProcessById(id);
    }

    /**
     * 查询项目数据工序列表
     *
     * @param projectDataProcess 项目数据工序
     * @return 项目数据工序
     */
    @Override
    public List<ProjectDataProcess> selectProjectDataProcessList(ProjectDataProcess projectDataProcess) {
        return projectDataProcessMapper.selectProjectDataProcessList(projectDataProcess);
    }

    /**
     * 新增项目数据工序
     *
     * @param projectDataProcess 项目数据工序
     * @return 结果
     */
    @Override
    public int insertProjectDataProcess(ProjectDataProcess projectDataProcess) {
        preCheck(projectDataProcess, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectDataProcess.setCreateBy(loginUser.getUsername());
        projectDataProcess.setCreateTime(DateUtils.getNowDate());
        return projectDataProcessMapper.insertProjectDataProcess(projectDataProcess);
    }

    /**
     * 修改项目数据工序
     *
     * @param projectDataProcess 项目数据工序
     * @return 结果
     */
    @Override
    public int updateProjectDataProcess(ProjectDataProcess projectDataProcess) {
        preCheck(projectDataProcess, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectDataProcess.setUpdateBy(loginUser.getUsername());
        projectDataProcess.setUpdateTime(DateUtils.getNowDate());
        return projectDataProcessMapper.updateProjectDataProcess(projectDataProcess);
    }

    /**
     * 批量删除项目数据工序
     *
     * @param ids 需要删除的项目数据工序主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataProcessByIds(Long[] ids) {
        return projectDataProcessMapper.deleteProjectDataProcessByIds(ids);
    }

    /**
     * 删除项目数据工序信息
     *
     * @param id 项目数据工序主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataProcessById(Long id) {
        return projectDataProcessMapper.deleteProjectDataProcessById(id);
    }

    /**
     * 添加、修改前检查
     *
     * @param projectDataProcess 数据
     * @param isAdd              是否为添加
     * @return 返回结果数据
     */
    private ProjectDataProcess preCheck(ProjectDataProcess projectDataProcess, boolean isAdd) {
        // 检查data是否在project中存在
        ProjectData projectDataQuery = new ProjectData();
        projectDataQuery.setProjectId(projectDataProcess.getProjectId());
        projectDataQuery.setDataId(projectDataProcess.getDataId());
        List<ProjectData> projectDataQueryList = projectDataMapper.selectProjectDataList(projectDataQuery);
        if (projectDataQueryList.size() == 0) {
            throw new ServiceException("该数据在项目中不存在, 无法为其添加工序");
        }

        // 检查是否有重复信息，联合主键projectId和dataId processId
        ProjectDataProcess projectDataProcessQuery = new ProjectDataProcess();
        projectDataProcessQuery.setProjectId(projectDataProcess.getProjectId());
        projectDataProcessQuery.setDataId(projectDataProcess.getDataId());
        projectDataProcessQuery.setProcessId(projectDataProcess.getProcessId());
        List<ProjectDataProcess> queryList = projectDataProcessMapper.selectProjectDataProcessList(projectDataProcessQuery);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该数据工序在项目中已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), projectDataProcess.getId())) {
                throw new ServiceException("该数据工序在项目中已经存在");
            }
        }

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(projectDataProcess.getProjectId());
        projectDataProcess.setProjectCode(project.getCode());
        projectDataProcess.setProjectName(project.getName());

        // 填充data的code、name信息
        Data data = dataMapper.selectDataById(projectDataProcess.getDataId());
        projectDataProcess.setDataCode(data.getCode());
        projectDataProcess.setDataName(data.getName());

        // 填充process的code、name信息
        Process process = processMapper.selectProcessById(projectDataProcess.getProcessId());
        projectDataProcess.setProcessCode(process.getCode());
        projectDataProcess.setProcessName(process.getName());

        return projectDataProcess;
    }
}
